Socket
Socket
Sign inDemoInstall

@blakek/curry

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@blakek/curry

🍛 Simple curry functions


Version published
Weekly downloads
9.4K
decreased by-34.27%
Maintainers
1
Weekly downloads
 
Created
Source

curry

🍛 Simple curry functions

A simple helper to wrap a function and gradually accept (i.e. partially apply) arguments.

This package is technically a helper partial application, but curry is much easier to remember and a nicer package function name than partiallyApply.

Install

Using Yarn:

$ yarn add @blakek/curry

…or using npm:

$ npm i --save @blakek/curry

Usage

import { curry, curryRight } from '@blakek/curry';

// Simple curry usage
const multiply = (a, b) => a * b;
const timesTwo = curry(multiply)(2);
timesTwo(5); //» 10

// Specify number of arguments
const sum = (...args) => args.reduce((a, b) => a + b, 0);
const topFiveTotal = curry(sum, 5);
topFiveTotal(1, 5, 6)(6)(7); //» 25

// Reverse arguments
const prop = (object, key) => object[key];
const getName = curryRight(prop)('name');
getName({ name: 'John Smith', age: 42 }); //» 'John Smith'

TypeScript

The types of many functions are inferred. However, some functions, such as variadic functions, need an explicit type:

const add = (...args: number[]) => args.reduce((a, b) => a + b, 0);

const addFourItems = curry(add, 4) as VariadicCurry<
  // argument types
  [number, number, number, number],
  // return type
  number
>;

// Includes helpers for functions accepting <=5 args
const addTwo = curry(add, 2) as Curry2<number, number, number>;

API

curry

curry(fn: Function, arity?: number): any;

Returns functions that take in the arguments of a function until all have been provided.

curryRight

curryRight(fn: Function, arity?: number): any;

Same as curry, but reverses all arguments once the limit has been provided. Provided for the common use-case of needing to supply context-specific arguments before providing the first arguments.

For example:

import { curryRight } from '@blakek/curry';

const users = [
  { username: 'blakek' },
  { username: 'gsandf' },
  { username: 'google' }
];

const prop = (object, key) => object[key];
const getUsername = curryRight(prop)('username');
users.map(getUsername); //» [ 'blakek', 'gsandf', 'google' ]

Contributing

Node.js and Yarn are required to work with this project.

To install all dependencies, run:

yarn

Useful Commands

yarn buildBuilds the project to ./dist
yarn formatFormat the source following the Prettier styles
yarn testRun project tests
yarn test --watchRun project tests, watching for file changes

License

MIT

Keywords

FAQs

Package last updated on 15 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc